package com.example.lib_mvp.views;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import java.util.Random;

public class ShakingCylinderView extends View {
    private static final int CYLINDER_COUNT = 3;
    // 动画最短持续时间，单位：毫秒
    private static final int ANIMATION_DURATION_MIN = 500;
    // 动画最长持续时间，单位：毫秒
    private static final int ANIMATION_DURATION_MAX = 2000;
    private static final int MAX_CYLINDER_HEIGHT = 86;
    private static final int MIN_CYLINDER_HEIGHT = 20;
    private static final int CYLINDER_SPACING = 10;
    private static final float CORNER_RADIUS = 10f;
    // 最小伸缩比例调整为 1/3
    private static final float MIN_SCALE = 1f / 2;
    private static final float MAX_SCALE = 1.2f;
    private int cylinderColor=Color.parseColor("#C4C4C4");
    private Paint paint;
    private int[] cylinderHeights;
    // 每个柱子的伸缩比例
    private float[] cylinderScales;
    private Random random;
    private ValueAnimator[] animators;

    public ShakingCylinderView(Context context) {
        this(context, null);
    }

    public ShakingCylinderView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ShakingCylinderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void setCylinderColor(int cylinderColor) {
        this.cylinderColor = cylinderColor;
    }

    private void init() {
        paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
        cylinderHeights = new int[CYLINDER_COUNT];
        cylinderScales = new float[CYLINDER_COUNT];
        random = new Random();
        animators = new ValueAnimator[CYLINDER_COUNT];

        for (int i = 0; i < CYLINDER_COUNT; i++) {
            cylinderHeights[i] = random.nextInt(MAX_CYLINDER_HEIGHT - MIN_CYLINDER_HEIGHT) + MIN_CYLINDER_HEIGHT;
            cylinderScales[i] = MIN_SCALE;
            setupAnimator(i);
        }
    }

    private void setupAnimator(int index) {
        // 随机设置每个柱子的动画时长
        int animationDuration = random.nextInt(ANIMATION_DURATION_MAX - ANIMATION_DURATION_MIN) + ANIMATION_DURATION_MIN;
        animators[index] = ValueAnimator.ofFloat(MIN_SCALE, MAX_SCALE);
        animators[index].setDuration(animationDuration);
        // 使用 AccelerateDecelerateInterpolator 插值器，让动画有加速和减速效果
        animators[index].setInterpolator(new LinearInterpolator());
        animators[index].setRepeatCount(ValueAnimator.INFINITE);
        animators[index].setRepeatMode(ValueAnimator.REVERSE);
        animators[index].addUpdateListener(animation -> {
            cylinderScales[index] = (float) animation.getAnimatedValue();
            // 减少随机更新高度的频率，例如在动画开始或结束时更新
            if (cylinderScales[index] == MIN_SCALE || cylinderScales[index] == MAX_SCALE) {
                cylinderHeights[index] = random.nextInt(MAX_CYLINDER_HEIGHT - MIN_CYLINDER_HEIGHT) + MIN_CYLINDER_HEIGHT;
            }
            invalidate();
        });
        animators[index].start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(cylinderColor);
        int width = getWidth();
        int totalSpacing = (CYLINDER_COUNT - 1) * CYLINDER_SPACING;
        int cylinderWidth = (width - totalSpacing) / CYLINDER_COUNT;

        for (int i = 0; i < CYLINDER_COUNT; i++) {
            int left = i * (cylinderWidth + CYLINDER_SPACING);
            int centerY = getHeight() / 2;
            int halfHeight = (int) (cylinderHeights[i] * cylinderScales[i] / 2);
            int top = centerY - halfHeight;
            int bottom = centerY + halfHeight;
            int right = left + cylinderWidth;

            RectF rectF = new RectF(left, top, right, bottom);
            canvas.drawRoundRect(rectF, CORNER_RADIUS, CORNER_RADIUS, paint);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (animators != null) {
            for (ValueAnimator animator : animators) {
                if (animator != null && animator.isRunning()) {
                    animator.cancel();
                }
            }
        }
    }
}